package com.yan01.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author yyh
 * @create 2020-10-31 9:48
 */
public class NioFileChannel01 {

    public static void main(String[] args) throws Exception {
        String src = "hello,yanyuhang";
        //创建一个输出流->channel
        FileOutputStream fileoutputStream = new FileOutputStream("d:\\12345.txt");

        //通过 fileOutputStream 获取 对应的 FileChannel
        //这个 fileChannel 真实 类型是  FileChannelImpl

        FileChannel channel = fileoutputStream.getChannel();

        //创建一个缓冲区 ByteBuffer
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        //将 src 放入 byteBuffer
        allocate.put(src.getBytes());
        //对byteBuffer 进行flip
        allocate.flip();
        //将byteBuffer 数据写入到 fileChannel
        channel.write(allocate);
        fileoutputStream.close();

    }


}
